 package com.es.util.upload;
 
 import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
 
 @SuppressWarnings("serial")
public class Upload extends HttpServlet{
	 

	 public void init(ServletConfig servletConfig) throws ServletException {
		    super.init(servletConfig);

		    if(System.getProperty("appDir")==null){
			    String appDir = servletConfig.getServletContext().getRealPath("/");
			    System.getProperties().put("appDir", appDir);
		    }
	 }		    

	 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		 System.getProperty("appDir");
     SmartUpload mySmartUpload = new SmartUpload();
     mySmartUpload.initialize(getServletConfig(), request, response);
     String imp_name = "";
     String excel_type = "";
     String action_url = "";
     String filenames = "";
     String intime = "";
     String error="";
     Boolean hasError = false;
     try{
    	 mySmartUpload.upload();
    	 Request req = mySmartUpload.getRequest();
    	 
    	 imp_name = req.getParameter("jsp_url");
    	 excel_type = req.getParameter("excel_type");
    	 action_url = /*request.getContextPath()+*/request.getServletPath().replace(".upload", ".action");
    	 intime = req.getParameter("intime").toString().trim();
    	 
    	 if (excel_type==null||excel_type.equals("")){
	          error = "服务器内部错误：未指定当前页面的附件保存位置，请指定excel_type的值";
	          hasError = true;
    	 }
    	
    	 for (int i = 0; i < mySmartUpload.getFiles().getCount(); ++i){
    		 File file = mySmartUpload.getFiles().getFile(i);
    		 if (!file.isMissing()){    			 
    			 String ext = mySmartUpload.getFiles().getFile(i).getFileExt();
    			 if (!ext.equalsIgnoreCase("xls")){
    				error = "文件格式不符！";
    				hasError = true;
    				break;
    			 }
    			 if (file.getSize() > 4194304){
    				 error = "文件大于4M，请处理后再次发送！";
			         hasError = true;
			         break;
    			 }

    			 String fileName = excel_type.trim()+"-"+Long.toString(System.currentTimeMillis()) + i + "." + ext; 
    			 file.saveAs(System.getProperty("appDir")+"upload\\" + fileName, mySmartUpload.SAVE_PHYSICAL);
    			 filenames += "," + fileName;
    		 }
    	 }
     }catch (Exception e) {
    	 e.printStackTrace();
     } 
  
     if(hasError){
    	 request.setAttribute("message", error);
         RequestDispatcher dis = request.getRequestDispatcher(imp_name);
         dis.forward(request, response);
         return;
     }
     request.setAttribute("fileName", filenames.replaceFirst(",", ""));
     request.setAttribute("intime", intime);

     RequestDispatcher dis = request.getRequestDispatcher(action_url);
     dis.forward(request, response);
   }
 
   protected void doPost(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException{
     doGet(request, response);
   }
 }